package cn.iocoder.yudao.module.wine.controller.admin.messageboard;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.messageboard.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.messageboard.MessageBoardDO;
import cn.iocoder.yudao.module.wine.service.messageboard.MessageBoardService;

@Tag(name = "管理后台 - 用户留言板")
@RestController
@RequestMapping("/wine/message-board")
@Validated
public class MessageBoardController {

    @Resource
    private MessageBoardService messageBoardService;

    @PostMapping("/create")
    @Operation(summary = "创建用户留言板")
    @PreAuthorize("@ss.hasPermission('wine:message-board:create')")
    public CommonResult<Long> createMessageBoard(@Valid @RequestBody MessageBoardSaveReqVO createReqVO) {
        return success(messageBoardService.createMessageBoard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户留言板")
    @PreAuthorize("@ss.hasPermission('wine:message-board:update')")
    public CommonResult<Boolean> updateMessageBoard(@Valid @RequestBody MessageBoardSaveReqVO updateReqVO) {
        messageBoardService.updateMessageBoard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户留言板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:message-board:delete')")
    public CommonResult<Boolean> deleteMessageBoard(@RequestParam("id") Long id) {
        messageBoardService.deleteMessageBoard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户留言板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:message-board:query')")
    public CommonResult<MessageBoardRespVO> getMessageBoard(@RequestParam("id") Long id) {
        MessageBoardDO messageBoard = messageBoardService.getMessageBoard(id);
        return success(BeanUtils.toBean(messageBoard, MessageBoardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户留言板分页")
    @PreAuthorize("@ss.hasPermission('wine:message-board:query')")
    public CommonResult<PageResult<MessageBoardRespVO>> getMessageBoardPage(@Valid MessageBoardPageReqVO pageReqVO) {
        PageResult<MessageBoardRespVO> pageResult = messageBoardService.getMessageBoardPage(pageReqVO);
        return success(pageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出用户留言板 Excel")
//    @PreAuthorize("@ss.hasPermission('wine:message-board:export')")
//    @OperateLog(type = EXPORT)
//    public void exportMessageBoardExcel(@Valid MessageBoardPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<MessageBoardDO> list = messageBoardService.getMessageBoardPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "用户留言板.xls", "数据", MessageBoardRespVO.class,
//                        BeanUtils.toBean(list, MessageBoardRespVO.class));
//    }

}
